package com.sacfa.commons.bo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.log4j.Logger;

import com.sacfa.commons.beans.Opcion;
import com.sacfa.commons.connection.PoolConexion;
import com.sacfa.commons.sql.TipoEmailSQL;
import com.sacfa.commons.utilities.SACFAExceptions;

public class TipoEmailBo {
	private static Logger log = Logger.getLogger(TipoEmailBo.class);
	
	public void registrar(Opcion tipoEmail){
		PreparedStatement stm = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			PoolConexion.getInstance().beginTransaction(conex);
			
			stm = conex.prepareStatement(TipoEmailSQL.INSERT);
			stm.setString(1, tipoEmail.getDescripcion());
			stm.executeUpdate();
			
			PoolConexion.getInstance().commit(conex);
			PoolConexion.getInstance().endTransaction(conex);
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
	}

	public Opcion consultar(Opcion tipoEmail){
		PreparedStatement stm = null;
		ResultSet rs = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			stm = conex.prepareStatement(TipoEmailSQL.SELECT_ONE);
			stm.setInt(1, tipoEmail.getId());
			rs = stm.executeQuery();
			
			rs.next();
			tipoEmail.setId(rs.getInt(1));
			tipoEmail.setDescripcion(rs.getString(2));
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
		return tipoEmail;
	}
	
	public Collection<Opcion> consultaGeneral(){
		Collection<Opcion> tipoEmails = new ArrayList<Opcion>();
		Opcion tipoEmail = null;
		PreparedStatement stm = null;
		ResultSet rs = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			stm = conex.prepareStatement(TipoEmailSQL.SELECT_ALL);
			rs = stm.executeQuery();
			
			while(rs.next()){
				tipoEmail = new Opcion();
				tipoEmail.setId(rs.getInt(1));
				tipoEmail.setDescripcion(rs.getString(2));
				tipoEmails.add(tipoEmail);
			}
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
		return tipoEmails;
	}
	
	public void modificar(Opcion tipoEmail){
		PreparedStatement stm = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			PoolConexion.getInstance().beginTransaction(conex);
			
			stm = conex.prepareStatement(TipoEmailSQL.UPDATE);
			stm.setString(1, tipoEmail.getDescripcion());
			stm.setInt(2, tipoEmail.getId());
			stm.executeUpdate();
			
			PoolConexion.getInstance().commit(conex);
			PoolConexion.getInstance().endTransaction(conex);
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
	}
	
	public void eliminar(Opcion tipoEmail){
		PreparedStatement stm = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			PoolConexion.getInstance().beginTransaction(conex);
			
			stm = conex.prepareStatement(TipoEmailSQL.DELETE);
			stm.setInt(1, tipoEmail.getId());
			stm.executeUpdate();
			
			PoolConexion.getInstance().commit(conex);
			PoolConexion.getInstance().endTransaction(conex);
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
	}
}
